<?php

/**
 * This is the model class for table "posts".
 *
 * The followings are the available columns in table 'posts':
 * @property integer $post_id
 * @property string $post_title
 * @property string $alias
 * @property string $post_description
 * @property string $post_content
 * @property string $post_type
 * @property integer $parent_id
 * @property integer $author_id
 * @property string $updater_id
 * @property integer $status
 * @property string $tags
 * @property integer $created
 * @property integer $updated
 * @property string $meta_keywords
 * @property string $meta_description
 * @property string $images
 * @property integer $sticky
 *
 * The followings are the available model relations:
 * @property PostComment[] $postComments
 * @property Users $author
 */
class Post extends CActiveRecord {

    const STATUS_PUBLISH = 1;
    const STATUS_DRAFT = 2;
    const STATUS_ARCHIVE = 3;
    const STATUS_DELETE = 0;

    const STICKY_YES = 1;
    const STICKY_NO = 0;

    public $imageArray = array();

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Post the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'posts';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('post_title, alias, post_type, status', 'required'),
            array('parent_id, author_id, status, created, updated, sticky', 'numerical', 'integerOnly' => true),
            array('post_title, alias, meta_keywords, meta_description', 'length', 'max' => 255),
            array('post_type', 'length', 'max' => 25),
            array('updater_id', 'length', 'max' => 11),
            array('status', 'in', 'range' => array(self::STATUS_ARCHIVE, self::STATUS_DELETE, self::STATUS_DRAFT, self::STATUS_PUBLISH)),
            array('sticky', 'in', 'range' => array(self::STICKY_NO, self::STICKY_YES)),
            array('post_description, post_content, tags, images', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('post_id, post_title, alias, post_description, post_content, status, tags, sticky', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'postComments' => array(self::HAS_MANY, 'PostComment', 'post_id'),
            'author' => array(self::BELONGS_TO, 'Users', 'author_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'post_id' => 'Post',
            'post_title' => 'Post Title',
            'alias' => 'Alias',
            'post_description' => 'Post Description',
            'post_content' => 'Post Content',
            'post_type' => 'Post Type',
            'parent_id' => 'Parent',
            'author_id' => 'Author',
            'updater_id' => 'Updater',
            'status' => 'Status',
            'tags' => 'Tags',
            'created' => 'Created',
            'updated' => 'Updated',
            'meta_keywords' => 'Meta Keywords',
            'meta_description' => 'Meta Description',
            'images' => 'Images',
            'sticky' => 'Sticky',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('post_id', $this->post_id);
        $criteria->compare('post_title', $this->post_title, true);
        $criteria->compare('post_description', $this->post_description, true);
        $criteria->compare('post_content', $this->post_content, true);
        $criteria->compare('author_id', $this->author_id);
        $criteria->compare('updater_id', $this->updater_id, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('tags', $this->tags, true);
        $criteria->compare('sticky', $this->sticky);
        $criteria->compare('post_type', $this->post_type);
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => param('pageSize')
            )
        ));
    }

    /**
     * Actions to be taken before saving the record.
     * @return boolean
     */
    public function beforeSave() {
        if (parent::beforeSave()) {
            if($this->isNewRecord){
                $this->created = $this->updated = time();

                $this->author_id = user()->id;
            }else{
                $this->updater_id = user()->id;
            }

            //create slug
            $this->alias = StringHelper::encode($this->alias, '-', false);

            return true;
        }

        return false;
    }

    /**
     * get images after decode
     * @return array
     */
    public function getImages($fullPath = true){
        if($this->images != ''){
            if($fullPath){
                $images = CJSON::decode($this->images);

                foreach($images as &$image){
                    $image = UPLOAD_URL . $image;
                }

                return $images;
            }else{
                return CJSON::decode($this->images);
            }
        }

        return array();
    }

    /**
     * get first image
     * @return string
     */
    public function getFeaturedImageUrl($thumb = FALSE, $fullPath = true){
        if($this->images != ''){
            $images = CJSON::decode($this->images);

            if($thumb){
                return ImageHelper::thumb(200, 200, $images[0]);
            }

            if($fullPath){
                return UPLOAD_URL . $images[0];
            }

            return $images[0];
        }

        return array();
    }

    /**
     * get full url
     */
    public function getUrl(){
        switch ($this->post_type){
            case 'post' :
                $url = url('/blog/post/view', array('alias' => $this->alias));
                break;
            default :
                $url = '';
                break;
        }

        return $url;
    }
}